﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Classes.inventory {
    import flash.utils.*;
    import Classes.*;
    import Display.*;
    import com.adobe.utils.*;

    public class Inventory {

        private var m_items:Dictionary;
        private var m_count:int = 0;

        public function Inventory(_arg1:Player, _arg2:Object){
            var _local3:String;
            var _local4:int;
            this.m_items = new Dictionary();
            super();
            if ((((((_arg2 == null)) || ((_arg2.count == null)))) || ((_arg2.items == null)))){
                return;
            };
            this.m_count = 0;
            for (_local3 in _arg2.items) {
                _local4 = int(_arg2.items[_local3]);
                this.addItems(_local3, _local4, true, false);
            };
        }
        public function get totalCount():int{
            return (this.m_count);
        }
        public function get capacity():int{
            return (Global.gameSettings.getInt("maxInventoryCapacity", 0));
        }
        public function get spareCapacity():int{
            return ((this.capacity - this.totalCount));
        }
        public function getItemCountByName(_arg1:String):int{
            var _local2:Item = Global.gameSettings.getItemByName(_arg1);
            return (((_local2)!=null) ? this.getItemCount(_local2) : 0);
        }
        public function getItemCount(_arg1:Item):int{
            if (this.m_items[_arg1] != null){
                return (int(this.m_items[_arg1]));
            };
            return (0);
        }
        public function getItems():Array{
            return (DictionaryUtil.getKeys(this.m_items));
        }
        public function getItemCountDictionaryByType(_arg1:String):Dictionary{
            var _local4:Item;
            var _local2:Dictionary = new Dictionary();
            var _local3:Array = this.getItems();
            for each (_local4 in _local3) {
                if (_local4.type != _arg1){
                } else {
                    _local2[_local4] = this.m_items[_local4];
                };
            };
            return (_local2);
        }
        public function getItemCountTotalByType(_arg1:String):int{
            var _local4:String;
            var _local2:Dictionary = this.getItemCountDictionaryByType(_arg1);
            var _local3:int;
            for (_local4 in _local2) {
                _local3 = (_local3 + _local2[_local4]);
            };
            return (_local3);
        }
        public function isItemCountBelowInventoryLimit(_arg1:String, _arg2:int):Boolean{
            if ((((_arg2 <= 0)) || ((this.spareCapacity < _arg2)))){
                return (false);
            };
            var _local3:Item = Global.gameSettings.getItemByName(_arg1);
            if (_local3 == null){
                return (false);
            };
            if (this.m_items[_local3] == null){
                this.m_items[_local3] = 0;
            };
            if (_local3.inventoryLimit > 0){
                if ((this.m_items[_local3] + _arg2) > _local3.inventoryLimit){
                    if (this.m_items[_local3] < _local3.inventoryLimit){
                        return (true);
                    };
                    return (false);
                };
            };
            return (true);
        }
        public function addItems(_arg1:String, _arg2:int, _arg3:Boolean=false, _arg4:Boolean=true):Boolean{
            if ((((_arg2 <= 0)) || ((((this.spareCapacity < _arg2)) && (!(_arg3)))))){
                return (false);
            };
            var _local5:Item = Global.gameSettings.getItemByName(_arg1);
            if (_local5 == null){
                return (false);
            };
            if (this.m_items[_local5] == null){
                this.m_items[_local5] = 0;
            };
            if ((((_local5.inventoryLimit > 0)) && (!(_arg3)))){
                if ((this.m_items[_local5] + _arg2) > _local5.inventoryLimit){
                    if (this.m_items[_local5] < _local5.inventoryLimit){
                        this.m_count = (this.m_count + (_local5.inventoryLimit - this.m_items[_local5]));
                        this.m_items[_local5] = _local5.inventoryLimit;
                        return (true);
                    };
                    if (_arg4){
                        UI.displayInventoryMaxExceededDialog();
                        return (false);
                    };
                };
            };
            this.m_items[_local5] = (this.m_items[_local5] + _arg2);
            this.m_count = (this.m_count + _arg2);
            if (Global.ui.inventoryView != null){
                UI.deinitializeInventory();
            };
            return (true);
        }
        public function addSingletonItem(_arg1:String, _arg2:Boolean=false):Boolean{
            var _local3:Boolean;
            if (this.verifySingletonItem(_arg1)){
                _local3 = this.addItems(_arg1, 1, _arg2);
            };
            return (_local3);
        }
        public function verifySingletonItem(_arg1:String):Boolean{
            var _local5:Array;
            var _local6:ConstructionSite;
            var _local7:Array;
            var _local8:Mall;
            var _local9:MapResource;
            var _local2:Boolean;
            var _local3:Array = Global.gameSettings.getItemNamesInUpgradeHeirarchy(_arg1);
            var _local4 = "";
            if (!_local2){
                for each (_local4 in _local3) {
                    _local2 = (this.getItemCountByName(_local4) > 0);
                    if (_local2){
                        break;
                    };
                };
            };
            if (!_local2){
                for each (_local4 in _local3) {
                    _local2 = (Global.player.storageComponent.getItemsByName(_local4).length > 0);
                    if (_local2){
                        break;
                    };
                };
            };
            if (!_local2){
                _local2 = (Global.world.getObjectsByNames(_local3).length > 0);
            };
            if (!_local2){
                _local5 = Global.world.getObjectsByClass(ConstructionSite);
                for each (_local6 in _local5) {
                    for each (_local4 in _local3) {
                        _local2 = (_local6.targetName == _local4);
                        if (_local2){
                            //unresolved jump
                        };
                    };
                };
            };
            if (!_local2){
                _local7 = Global.world.getObjectsByClass(Mall);
                for each (_local8 in _local7) {
                    for each (_local9 in _local8.slots) {
                        if (_local9){
                            for each (_local4 in _local3) {
                                _local2 = (_local9.getItem().name == _local4);
                                if (_local2){
                                    //unresolved jump
                                };
                            };
                        };
                    };
                };
            };
            return (!(_local2));
        }
        public function removeItems(_arg1:String, _arg2:int, _arg3:Boolean=true):Boolean{
            if ((((_arg2 <= 0)) || ((_arg2 > this.getItemCountByName(_arg1))))){
                return (false);
            };
            var _local4:Item = Global.gameSettings.getItemByName(_arg1);
            if (_local4 == null){
                return (false);
            };
            this.m_count = (this.m_count - _arg2);
            this.m_items[_local4] = (this.m_items[_local4] - _arg2);
            if (this.m_items[_local4] == 0){
                delete this.m_items[_local4];
            };
            if (((!((Global.ui.inventoryView == null))) && (_arg3))){
                UI.refreshInventory();
            };
            return (true);
        }

    }
}//package Classes.inventory 
